package cn.formal.controller;

import cn.formal.entity.PageResult;
import cn.formal.entity.Result;
import cn.formal.pojo.QuestionAnswer;
import cn.formal.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("搜索服务")
@Slf4j
@RestController
@RequestMapping("question")
public class QuestionAnswerController {


    public static final String QUESTION_ANSWER_FIND_SUCCESS = "获取问答成功";
    public static final String QUESTION_ANSWER_FIND_FAIL = "获取问答失败";
    public static final String QUESTION_ANSWER_ADD_DOC_SUCCESS = "新增文档成功";
    public static final String QUESTION_ANSWER_ADD_DOC_FAIL = "新增文档失败";
    public static final String QUESTION_ANSWER_UPDATE_DOC_SUCCESS = "修改文档成功";
    public static final String QUESTION_ANSWER_UPDATE_DOC_FAIL = "修改文档失败";
    public static final String QUESTION_ANSWER_DELETE_DOC_SUCCESS = "删除文档成功";
    public static final String QUESTION_ANSWER_DELETE_DOC_FAIL = "删除文档失败";



    @Autowired
    private SearchService searchService;

    /*
    添加文档
     */
    @ApiOperation(value = "添加问答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "answer",value = "QuestionAnswer实体类")
    })
    @PostMapping("addDoc")
    public Result addDoc(@RequestBody QuestionAnswer answer){
        log.info("[addDoc接收前端消息] - answer: {}",answer);
        searchService.addDoc(answer);
        return new Result(true,QUESTION_ANSWER_ADD_DOC_SUCCESS);
    }

    /*
    根据id查找
     */
    @ApiOperation(value = "根据id查找问答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "问答id")
    })
    @GetMapping("findById")
    public Result findById(Long id){
        QuestionAnswer questionAnswer = searchService.findById(id);
        return new Result(true,QUESTION_ANSWER_FIND_SUCCESS,questionAnswer);
    }

    /*
    修改文档
     */
    @ApiOperation(value = "更新问答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "answer",value = "QuestionAnswer实体类")
    })
    @PostMapping("update")
    public Result update(@RequestBody QuestionAnswer answer){
        searchService.update(answer);
        return new Result(true,QUESTION_ANSWER_UPDATE_DOC_SUCCESS);
    }

    /*
    删除文档
     */
    @ApiOperation(value = "根据id删除问答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "问答id")
    })
    @GetMapping("deleteById")
    public Result deleteById(Long id){
        searchService.deleteById(id);
        return new Result(true,QUESTION_ANSWER_DELETE_DOC_SUCCESS);
    }

    /*
    删除所有文档
     */
    @ApiOperation(value = "删除所有问答")
    @GetMapping("deleteAll")
    public Result deleteAll(){
        searchService.deleteAll();
        return new Result(true,QUESTION_ANSWER_DELETE_DOC_SUCCESS);
    }

    /*
    根据title查找
     */
    @ApiOperation(value = "根据title查找符合条件的问答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "问题/问题关键词")
    })
    @GetMapping("findByTitle")
    public Result findByTitle(String title){
        List<QuestionAnswer> answerList = searchService.findByTitle(title);
        if (answerList!=null){
            return new Result(true,QUESTION_ANSWER_FIND_SUCCESS,answerList);
        }
        return new Result(false,QUESTION_ANSWER_FIND_FAIL);
    }

    /*
    查询所有
     */
    @ApiOperation(value = "查找所有问答")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "从第几页展示"),
            @ApiImplicitParam(name = "size",value = "单页问答条数")
    })
    @GetMapping("findAll")
    public Result findAll(@RequestParam("page")Integer page,@RequestParam("size") Integer size){
        if (page==null){
            page = 0;
        }
        if (size==null||size==0){
            size = 4;
        }
        PageResult pageResult = searchService.findAll(page,size);
        return new Result(true,QUESTION_ANSWER_FIND_SUCCESS,pageResult);
    }


}
